package com.hmall.trade.listener;

import com.hmall.trade.domain.po.Order;
import com.hmall.trade.service.IOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
public class PayStatusListener {
    private final IOrderService orderService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "trade.pay.success.queue", durable = "true"),
            exchange = @Exchange(name = "pay.exchange", type = ExchangeTypes.DIRECT),
            key = "pay.success"

    ))
    public void listenPaySuccess(Long orderId) {
        log.info("收到订单成功消息，开始修改订单状态");
        //1.查询订单状态
        Order order = orderService.getById(orderId);
        //2.判断订单状态是否是未支付  如果不是未支付 就不做处理     这样如果是拒绝的订单就不处理，或者其他原因重复提交的订单也不做处理
        if (order == null || order.getStatus() != 1) {
            return;
        }
        //3.标记订单为已支付
        orderService.markOrderPaySuccess(orderId);
        log.info("订单状态修改成功");
    }
}
